package pl.pawluk.inz.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.access.AccessDeniedException;
import pl.pawluk.inz.exceptions.VLPException;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import java.io.Serializable;
import java.util.Locale;

/**
 * @author: toshiba
 * Date: 07.03.13
 * Time: 21:23
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseBean implements Serializable {
    private static final Log log = LogFactory.getLog(BaseBean.class);
    @Autowired
    private MessageSource messageSource;
    @Autowired
    private LocaleBean localeBean;
    private final UpdateActionListener updateSelectedRow = new UpdateActionListener();

    private class UpdateActionListener implements Serializable, ActionListener {
        public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
            try {
                insertOrUpdate();
            } catch (Throwable t) {
                handleException(t);
            }
        }
    }

    protected String getMessage(String key, Object... args) {
        return messageSource.getMessage(key, args, localeBean.getLocale());
    }

    protected void handleException(Throwable e) {
        if (e instanceof VLPException) {
            BaseBean.handleException((VLPException) e, messageSource);
        } else {
            BaseBean.handleException(e, messageSource);
        }
    }

    public static void handleException(VLPException e, MessageSource msg) {
        FacesContext ctx = FacesContext.getCurrentInstance();
        Locale locale = ctx.getViewRoot().getLocale();
        if (e.getCode() < 0) {
            log.error(e.getCause().getLocalizedMessage(), e.getCause());
            ctx.addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_FATAL, e.getLocalizedMessage(), e.getMessage()));
        } else {
            ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg.getMessage(e.getErrorCode(), e.getArgs(), locale), ""));
        }
    }

    public ActionListener getUpdateSelectedRow() {
        return updateSelectedRow;
    }


    public static void handleException(Throwable e, MessageSource msg) {
        log.error(e.getLocalizedMessage(), e);
        FacesContext ctx = FacesContext.getCurrentInstance();
        Locale locale = ctx.getViewRoot().getLocale();
        ctx.addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_FATAL, "Błąd", msg.getMessage(VLPException.TECHNICAL_ERROR_CODE,
                        new Object[]{e.getLocalizedMessage()}, locale)));
    }


    protected abstract void insertOrUpdate();

    public abstract void onRowUnselect(UnselectEvent event);
    public abstract void onRowSelect(SelectEvent event);
    public abstract void createNew();
    public abstract void delete();

}
